package com.filter;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.beans.Admin;
import com.beans.Adv;
import com.service.SysManager;
import com.util.CharTools;
import com.util.Const;
import com.vo.Site;

public class ConfigFilter implements Filter {
	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		if (req instanceof HttpServletRequest) {
			doFilter((HttpServletRequest) req, (HttpServletResponse) res, chain);
		} else {
			chain.doFilter(req, res);
		}
	}

	@SuppressWarnings("unchecked")
	public void doFilter(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		Admin a = (Admin) request.getSession().getAttribute(Const.ADMIN);
		String path = request.getContextPath();
		String url = request.getRequestURI();
		List<String> list = (List<String>) request.getSession().getAttribute(Const.ADMIN_URL);
		List<String> url_do = (List<String>) request.getSession().getServletContext().getAttribute(Const.URL_DO);
		if (path.length() > 0)
			url = url.substring(path.length() + 1, url.length());
		String T_url;
		boolean check = false;
		Iterator<String> it=url_do.iterator();
		while (it.hasNext()) {
			T_url = it.next();
			if (url.equals(T_url)) {
				check = true;
				break;
			}
		}
		if (check) {
			check = false;
			if (list != null)
				for (String v : list) {
					T_url = (String) v;
					if (T_url.equals(url)) {
						check = true;
						break;
					}
				}
			if (!check) {
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(
						"{\"success\":false,\"err\":\"没有操作权限！请管理员重新登录\"}");
				// response.sendRedirect(path);// 没有访问该url授权则跳转到根目录
				System.out.println("访问未授权URL：" + url);
				return;
			}
		}
		String query = request.getQueryString();
		if (query == null)
			query = "";
		else
			url = url + "?" + query;
		request.setAttribute("url", CharTools.Utf8URLdecode(url));
		chain.doFilter(request, response);
	}

	public void init(FilterConfig config) throws ServletException {
		if (config.getServletContext().getAttribute("site") == null) {
			try {
				Site site = (Site) config.getServletContext().getAttribute(
						"site");
				site = Site.getInstance();
				org.springframework.web.context.WebApplicationContext wac = null;
				wac = WebApplicationContextUtils
						.getRequiredWebApplicationContext(config
								.getServletContext());
				SysManager sys = (SysManager) wac.getBean("sysManager");
				site.setMenu(sys.getMenuDao().getMenuList());
				com.beans.Site s = (com.beans.Site) sys.getMenuDao().findAll(
						com.beans.Site.class).get(0);
				site.setAddress(s.getAddress());
				site.setCopyright(s.getCopyright());
				site.setDescription(s.getDescription());
				site.setKeywords(s.getKeywords());
				site.setLogo(s.getLogo());
				site.setName(s.getName());
				site.setAdvList(sys.getMenuDao().findAll(Adv.class));
				site.init();
				config.getServletContext().setAttribute(Const.URL_DO, sys.getAdminDao().getUrl());
				config.getServletContext().setAttribute("site", site);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
